package com.jhh.pay.heli.api;


import com.jhh.pay.heli.constant.ApiConstant;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

import java.util.concurrent.TimeUnit;

/**
 * Created by zhangqi on 2016/9/12.
 */
public class RetrofitUtils {

    private static Retrofit sRetrofit;
    private static OkHttpClient mOkHttpClient;
    private int defaultTime = 100;
    private static PayApi mApi;

    private RetrofitUtils(){}

    public static class Hide{
        public static RetrofitUtils sRetrofitUtil = new RetrofitUtils();
    }

    public static RetrofitUtils getInstance() {
        return Hide.sRetrofitUtil;
    }

    public PayApi build(){
        if(mOkHttpClient == null){
//            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            mOkHttpClient = new OkHttpClient.Builder()
//                    .addInterceptor(new RetryAndFollowUpInterceptor(mOkHttpClient,true))
//                    .followRedirects(false)
//                    .followSslRedirects(false)
                    .connectTimeout(defaultTime, TimeUnit.SECONDS)
                    .readTimeout(defaultTime, TimeUnit.SECONDS)
                    .writeTimeout(defaultTime, TimeUnit.SECONDS)
                    .build();
        }

        if(sRetrofit == null){
            sRetrofit = new Retrofit.Builder().baseUrl(ApiConstant.baseUrl)
                    .client(mOkHttpClient)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        if(mApi == null){
            mApi = sRetrofit.create(PayApi.class);
        }
        return mApi;
    }

}
